/*
 * @lc app=leetcode.cn id=974 lang=golang
 *
 * [974] 和可被 K 整除的子数组
 */
package Solutions

// @lc code=start
func subarraysDivByK(nums []int, k int) int {
	sum := 0
	var ans = 0
	mp := make(map[int]int, k+1)
	mp[0] = 1
	for _, v := range nums {
		sum += v
		sum = (sum%k + k) % k
		ans += mp[sum]
		mp[sum]++
	}
	return ans
}

// @lc code=end
